<template>

  <div id="register-form">
    <p>请填写以下信息进行注册</p>
    <el-form :model="registerForm" status-icon :rules="rules" ref="registerForm" label-width="120px" class="register-form">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="registerForm.username"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="registerForm.name"></el-input>
      </el-form-item>
      <el-form-item label="学号" prop="studentId">
        <el-input v-model="registerForm.studentId"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="registerForm.password" show-password></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input type="password" v-model="registerForm.confirmPassword" show-password></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('registerForm')">立即注册</el-button>
        <el-button @click="resetForm('registerForm')">重置</el-button>
      </el-form-item>
      <router-link :to="{name: 'Login'}">已有账号？点击这里登录</router-link>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Enroll',
  data() {
    return {
      registerForm: {
        username: '',
        name: '',
        studentId: '',
        password: '',
        confirmPassword: ''
      },
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        studentId: [
          { required: true, message: '请输入学号', trigger: 'blur' },
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }, { min: 6, max: 18, message: '密码长度应为6到18位', trigger: 'blur' }],
        confirmPassword: [{ required: true, message: '请再次输入密码', trigger: 'blur' }, { validator: this.validatePassword, trigger: 'blur' }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log('submitForm', this.registerForm);
          // TODO: 调用注册接口
          this.$axios.post('/sys/user/register', this.registerForm)
              .then(res => {
                if (res.data.code === 200) {
                  this.$message({
                    message: '恭喜你，注册成功',
                    type: 'success'
                  });
                  this.$router.push('/login');
                } else {
                  this.$message.error(res.data.msg);
                }
              });
        } else {
          console.log('注册失败');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    validatePassword(rule, value, callback) {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.registerForm.password) {
        callback(new Error('两次输入的密码不一致!'));
      } else {
        callback();
      }
    }
  }
};
</script>

<style scoped>
.register-form {
  width: 350px;
  margin: 150px auto;
}
</style>
